/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Brotli } from './brotli.js';
import { brotliData, compressResult, compressedResult } from './brotliData.js';

const brotil = new Brotli();
let compressText;
let compressedTextArray;

function compress_Test() {
  const upCompressStartTime = Date.now();
  compressText = brotil.compress(brotliData, 6);
  const upCompressEndTime = Date.now();
  if (compressText != compressResult) {
    throw Error('brotli_compress error');
  }
  print(`brotli_compress: ${upCompressEndTime - upCompressStartTime} ms`);
}

function decompress_Test() {
  let decompressedText;
  const codeDecompressStartTime = Date.now();
  decompressedText = brotil.decompress(compressText);
  const codeDecompressEndTime = Date.now();
  if (decompressedText != '') {
    throw Error('brotli_decompress error');
  }
  print(`brotli_decompress: ${codeDecompressEndTime - codeDecompressStartTime} ms`);
}

function compressArray_Test() {
  const upBuf = new ArrayBuffer(brotliData.length);
  const upBufView = new Uint8Array(upBuf);
  for (let i = 0; i < brotliData.length; i++) {
    upBufView[i] = brotliData.charCodeAt(i);
  }
  const upCompressStartTime = Date.now();
  compressedTextArray = brotil.compressArray(upBufView, 6);
  const upCompressEndTime = Date.now();
  if (JSON.stringify(compressedTextArray) != compressedResult) {
    throw Error('brotli_compressArray error');
  }
  print(`brotli_compressArray: ${upCompressEndTime - upCompressStartTime} ms`);
}

function decompressArray_Test() {
  let decompressedText;
  const codeDecompressStartTime = Date.now();
  decompressedText = brotil.decompressArray(compressedTextArray);
  const codeDecompressEndTime = Date.now();
  const codeRestoredStr = String.fromCharCode(...decompressedText);
  if (codeRestoredStr != brotliData) {
    throw Error('brotli_decompressArray error');
  }
  print(`brotli_decompressArray: ${codeDecompressEndTime - codeDecompressStartTime} ms`);
}

compress_Test();
decompress_Test();
compressArray_Test();
decompressArray_Test();


